package com.sky.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    // 实现具体的报表功能
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistic(LocalDate begin, LocalDate end) {

        List<LocalDate> days = new ArrayList<>();
        // 根据开始与结尾计算天数
        days.add(begin);
        while (true){
            if(begin.equals(end))
                break;
            begin = begin.plusDays(1);
            days.add(begin);
        }

        // 根据天数计算每天的营业额
        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate day : days) {
            LocalDateTime beginTime = LocalDateTime.of(day, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(day, LocalTime.MAX);

            Integer status = Orders.COMPLETED;

            HashMap<Object, Object> map = new HashMap<>();
            map.put("begin", beginTime);
            map.put("end", endTime);
            map.put("status", status);

            Double amouns = orderMapper.selectTurnoverByDay(map);
            amouns = amouns == null ? 0.0 : amouns;
            turnoverList.add(amouns);
        }

        return TurnoverReportVO.
                builder().
                dateList(StringUtils.join(days, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
    }

    /**
     * 统计新增用户与总用户
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {

        // 根据开始与结尾计算天数
        List<LocalDate> days = new ArrayList<>();
        days.add(begin);
        while (true){
            if(begin.equals(end))
                break;
            begin = begin.plusDays(1);
            days.add(begin);
        }

        // 根据天数计算用户量
        List<Integer> userCountList = new ArrayList<>();

        List<Integer> userAddList = new ArrayList<>();
        for (LocalDate day : days) {
            LocalDateTime beginTime = LocalDateTime.of(day, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(day, LocalTime.MAX);

            // 计算总用户量
            Integer userCount = userMapper.selectUserCountByDay(endTime);
            userCountList.add(userCount);
            // 计算每日新增用户量
            Integer userAdd = userMapper.selectUserAddCountByDay(beginTime, endTime);
            userAddList.add(userAdd);
        }
        // 将数组转换成， 分割的字符串
        String dates = StringUtils.join(days, ",");
        String userCountListStr = StringUtils.join(userCountList, ",");
        String userAddListStr = StringUtils.join(userAddList, ",");
        return new UserReportVO(dates,userCountListStr, userAddListStr);
    }

    /**
     * 查询订单数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO getOrdedrStatistics(LocalDate begin, LocalDate end) {


        // 根据开始与结尾计算天数
        List<LocalDate> days = new ArrayList<>();
        days.add(begin);
        while (true){
            if(begin.equals(end))
                break;
            begin = begin.plusDays(1);
            days.add(begin);
        }
        // 计算该时间内每一天的总订单与有效订单(已完成的订单)
        // 总的时间段内总订单以及有效订单以及订单完成率

        // 有效订单
        List<Integer> aListOfActiveOrdersList = new ArrayList<>();
        List<Integer> orderCountList = new ArrayList<>();
        int totalOrderCount = 0;
        int validOrderCount = 0;
        Double orderCompletionRate = 0.0;
        for (LocalDate day : days) {
            LocalDateTime beginTime = LocalDateTime.of(day, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(day, LocalTime.MAX);
            // 查询该天内的总订单
            int orderCount = orderMapper.selectCount(beginTime,endTime,null);
            totalOrderCount += orderCount;

            // 查询该天内的有效订单
            int activeOrderCount = orderMapper.selectCount(beginTime,endTime,Orders.COMPLETED);
            validOrderCount += activeOrderCount;

            aListOfActiveOrdersList.add(activeOrderCount);
            orderCountList.add(orderCount);
        }
        // 计算订单完成率
        orderCompletionRate = (double)validOrderCount / (double) totalOrderCount;

        // 包装数据
        log.info("总订单:{} 有效订单:{} 完成率{}",totalOrderCount,validOrderCount,orderCompletionRate);
        return new OrderReportVO(
                StringUtils.join(days,","),
                StringUtils.join(orderCountList,","),
                StringUtils.join(aListOfActiveOrdersList,","),
                totalOrderCount,validOrderCount,orderCompletionRate);
    }

    /**
     * 查询销量前十的商品
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO getTop10Statistics(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        // 通过SQL语句查询出 销量前十的菜品以及销售的数量
        List<GoodsSalesDTO> top10 = orderMapper.getTop10(beginTime,endTime);
        StringBuilder name = new StringBuilder();
        StringBuilder sales = new StringBuilder();
        // 封装成字符串返回给前端
        for (GoodsSalesDTO salesDTO : top10) {
            name.append(salesDTO.getName()).append(",");
            sales.append(salesDTO.getNumber()).append(",");
        }

        return new SalesTop10ReportVO(name.toString(),sales.toString());
    }
}
